package cn.edu.scau.cmi.xianjiehua.composite;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xianjiehua.composite.consitent.ConsitentComponent;
import cn.edu.scau.cmi.xianjiehua.composite.consitent.ConsitentOrganization;
import cn.edu.scau.cmi.xianjiehua.composite.consitent.ConsitentPerson;
import cn.edu.scau.cmi.xianjiehua.composite.safe.SafeComponent;
import cn.edu.scau.cmi.xianjiehua.composite.safe.SafeOrganzation;
import cn.edu.scau.cmi.xianjiehua.composite.safe.SafePerson;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

public class ViewController implements Initializable{
	
	@FXML
	private Button safeAdd;
	
	@FXML
	private Button safePrint;
	
	@FXML
	private Button consitentAdd;
	
	@FXML
	private Button consitentPrint;
	
	@FXML
	private TextArea console;
	
	private SafeComponent safeOrganization = null;
	private ConsitentComponent consitentOrganization = null;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		safeOrganization = new SafeOrganzation();
		consitentOrganization = new ConsitentOrganization();
	}
	
	public void safeAdd(ActionEvent event) {
		SafeComponent person = new SafePerson();
		if(safeOrganization instanceof SafeOrganzation) {
			SafeOrganzation org = (SafeOrganzation)safeOrganization;
			org.add(person);
			console.appendText(safeOrganization.getName() + " add " + person.getName() + " success!\n");
		}else {
			console.appendText(safeOrganization.getName() + " add " + person.getName() + " failure!\n");
		}
	}

	public void safePrint(ActionEvent event) {
		if(safeOrganization instanceof SafeOrganzation) {
			SafeOrganzation org = (SafeOrganzation)safeOrganization;
			ArrayList<SafeComponent> list = org.getAllPerson();
			if(list.isEmpty()) {
				console.appendText(safeOrganization.getName() + ": null\n");
			}else {
				console.appendText(safeOrganization.getName() + ":\n");
				for(SafeComponent component : list) {
					console.appendText(component.getName() + "\n");
				}
			}
		}else {
			throw new RuntimeException();
		}
	}
	
	public void consitentPrint(ActionEvent event) {
		if(consitentOrganization instanceof ConsitentComponent) {
			ConsitentComponent org = (ConsitentComponent)consitentOrganization;
			ArrayList<ConsitentComponent> list = org.getAllPerson();
			if(list.isEmpty()) {
				console.appendText(consitentOrganization.getName() + ": null\n");
			}else {
				console.appendText(consitentOrganization.getName() + ":\n");
				for(ConsitentComponent component : list) {
					console.appendText(component.getName() + "\n");
				}
			}
		}else {
			throw new RuntimeException();
		}
	}
	
	public void consitentAdd(ActionEvent event) {
		ConsitentComponent person = new ConsitentPerson();
		if(consitentOrganization instanceof ConsitentComponent) {
			ConsitentComponent org = (ConsitentComponent)consitentOrganization;
			org.add(person);
			console.appendText(consitentOrganization.getName() + " add " + person.getName() + " success!\n");
		}else {
			console.appendText(consitentOrganization.getName() + " add " + person.getName() + " failure!\n");
		}
	}

}
